package com.sgb.goods.view.activity;

import android.graphics.Canvas;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.utils.TextUtils;
import com.bumptech.glide.Glide;
import com.lidong.pdf.listener.OnDrawListener;
import com.lidong.pdf.listener.OnLoadCompleteListener;
import com.lidong.pdf.listener.OnPageChangeListener;
import com.sgb.goods.R;
import com.sgb.goods.databinding.GoodsActivityPdfReaderBinding;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.BaseActivity;
import com.sgb.goods.viewmodel.PdfReaderViewModel;

/**
 * pdf预览
 * @author cyj
 */
@Route(path = Constants.ARouter.AR_PREVIEW)
public class PdfReaderActivity extends BaseActivity<GoodsActivityPdfReaderBinding, PdfReaderViewModel> implements OnPageChangeListener, OnLoadCompleteListener, OnDrawListener {

    @Override
    protected int getLayoutResId() {
        return R.layout.goods_activity_pdf_reader;
    }

    @Override
    protected void init() {
        mDataBinding.head.title.setText("预览");
        String url = getIntent().getStringExtra("url");
        if (!TextUtils.isEmpty(url) && url.toLowerCase().contains(".pdf")) {
            mDataBinding.imgView.setVisibility(View.GONE);
            String fileUrl = getIntent().getStringExtra("fileUrl");
            if (fileUrl != null) {
                //设置pdf文件地址
                String[] dataArray = fileUrl.split("/");
                mDataBinding.pdfView.fileFromLocalStorage(
                        this,
                        this,
                        this,
                        url,
                        dataArray[dataArray.length - 1].toLowerCase().replace(".pdf", "")
                );
            }
        } else {
            mDataBinding.pdfView.setVisibility(View.GONE);
            try {
                Glide.with(this).load(url).into(mDataBinding.imgView);
            }catch (Exception e){

            }

        }
    }

    @Override
    protected void initListener() {
        mDataBinding.head.ivBack.setOnClickListener(v -> finish());
    }

    @Override
    public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int displayedPage) {

    }

    @Override
    public void loadComplete(int nbPages) {

    }

    @Override
    public void onPageChanged(int page, int pageCount) {

    }
}